package vip.xiaonuo.miniapp.api.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.Reactor;
import reactor.event.Event;
import reactor.function.Consumer;
import vip.xiaonuo.facedoor.modular.memberuser.entity.MemberUser;
import vip.xiaonuo.facedoor.modular.topic.EventTopic;
import vip.xiaonuo.miniapp.api.miniappmemberuser.entity.MiniappMemberUser;
import vip.xiaonuo.miniapp.api.miniappmemberuser.service.MiniappMemberUserService;

import static reactor.event.selector.Selectors.$;

/**
 * 访客机新增访客 监听 门禁设备增加
 */
@Service
@Slf4j
public class MemberDeleteListener implements Consumer<Event<MemberUser>>, InitializingBean {


    @Autowired
    private Reactor reactor;


    @Autowired
    private MiniappMemberUserService miniappMemberUserService;

    @Override
    public void accept(Event<MemberUser> event) {
        MemberUser data = event.getData();
        if (data.getMiniAppMemberId()!=null){
            miniappMemberUserService.removeById(data.getMiniAppMemberId());
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        reactor.on($(EventTopic.MEMBER_DELETE), this);
    }
}

